package test.yunpay.com.mvpproject;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;

import test.yunpay.com.mvpproject.presenter.IBasePresenter;

/**
 * Created by Administrator on 2018/5/4 0004.
 */

public abstract class BaseActivity extends AppCompatActivity {

        private IBasePresenter presenter = null;
        public Context mContext;

        @Override
        public void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(getLayoutResId());
            mContext = this;
            presenter = bindPresenter();
            initData();
            initView();
        }


        /**
         * 返回资源的布局
         *
         * @return
         */
        public abstract int getLayoutResId();

        /**
         * 组件初始化操作
         */
        public abstract void initView();

        /**
         * 页面初始化页面数据，在initView之后调用
         */
        public abstract void initData();

        /**
         * 绑定presenter，主要用于销毁工作
         *
         * @return
         */
        protected abstract IBasePresenter bindPresenter();

        /**
         * 如果重写了此方法，一定要调用super.onDestroy();
         * 避免内存泄漏
         */
        @Override
        public void onDestroy() {
            super.onDestroy();
            if (presenter != null) {
                presenter.onDestroy();
                presenter = null;
                System.gc();
            }
        }
    }

